package com.guizi.halftimesroom.utils;

import android.app.Activity;
import android.widget.TextView;

import com.google.gson.Gson;
import com.guizi.halftimesroom.bean.RecommandListNormanlBean;
import com.guizi.halftimesroom.bean.RecommandListSpecialBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecommandJsonUtil {
    private static List<RecommandListNormanlBean> mList ;
    private static RecommandListSpecialBean specialBean;
    private static Gson mGson  = new Gson();

    public static void getData(Activity activity, String url, final RecommandJsonListener listener) {
        mList= new ArrayList<>();
        OkhttpUtil.loadData(activity, url, new ResponseListener() {
            @Override
            public void onFilure() {
                listener.onFilure();
            }
            @Override
            public void onSuccess(String s) {
                try {
                    JSONObject object1=null;
                    JSONObject object2=null;
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray array= jsonObject.getJSONArray("data");
                    for (int i = 0; i <array.length() ; i++) {
                        if(i==0){
                            object1 =array.getJSONObject(i);
                        }else {
                            object2 =array.getJSONObject(i);
//                            String str=object2.toString();
                            specialBean = mGson.fromJson(object2.toString(), RecommandListSpecialBean.class);
                        }
                    }
                    JSONArray data = object1.getJSONArray("data");
                    for (int i = 0; i <data.length() ; i++) {
                        JSONObject object = data.getJSONObject(i);
                        RecommandListNormanlBean normanlBean = mGson.fromJson(object.toString(), RecommandListNormanlBean.class);
                        mList.add(normanlBean);
                    }
                    listener.onSuccess(mList,specialBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
